package ysq.xyg.web.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.web.dao.WebTagTypeDao;
import ysq.xyg.web.model.po.WebTagType;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.web.model.vo.WebTagTypeVo;
import ysq.xyg.web.service.WebTagTypeService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


/**
 * 网站模块-标签类型service实现
 *
 * @author shengQiang yu
 * @date 2019-6-7 20:44:17
 */
@Service("webTagTypeService")
public class WebTagTypeServiceImpl extends BaseServiceImpl<WebTagType> implements WebTagTypeService {

    @Resource
    private WebTagTypeDao webTagTypeDao;

    private WebTagType webTagType;

    @Autowired
    public void setBaseDao(WebTagTypeDao webTagTypeDao) {
        this.baseDao = webTagTypeDao;
    }


    /**
     * 分页查询
     *
     * @param webTagTypeVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-6-7 20:44:17
     */
    public ReturnEntity page(WebTagTypeVo webTagTypeVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" sort desc,update_time desc");
        List<WebTagType> list = acceptList(webTagTypeVo);
        PageInfo<WebTagType> page=new PageInfo<WebTagType>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param webTagTypeVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-6-7 20:44:17
     */
    public ReturnEntity addOrUpdate(WebTagTypeVo webTagTypeVo, SysUser currentUser) {
        boolean isUpate=true;
        if(StrUtil.isEmpty(webTagTypeVo.getId())){
            webTagType=new WebTagType(currentUser);
            isUpate=false;
        }else{
            webTagType=selectByPrimaryKey(webTagTypeVo.getId());
            if(ObjectUtil.isNull(webTagType)){
                return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
            }
            webTagType.setUpdateTime(new Date());
            webTagType.setUpdateUser(currentUser.getId());
        }
        webTagType.setName(webTagTypeVo.getName());
        webTagType.setTagType(webTagTypeVo.getTagType());
        webTagType.setSeoKeyword(webTagTypeVo.getSeoKeyword());
        webTagType.setDescription(webTagTypeVo.getDescription());
        webTagType.setSort(webTagTypeVo.getSort());
        webTagType.setShowType(webTagTypeVo.getShowType());
        webTagType.setUserId(currentUser.getId());
        int num=0;
        if(isUpate){
            num=updateByPrimaryKey(webTagType);
        }else{
            num=insert(webTagType);
        }
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }
    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-6-7 20:44:17
     */
    public ReturnEntity delete(String id, SysUser currentUser) {
        WebTagType webTagType=selectByPrimaryKey(id);
        if(webTagType!=null){
            webTagType.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            webTagType.setUpdateTime(new Date());
            webTagType.setUpdateUser(currentUser.getId());
            int num=updateByPrimaryKey(webTagType);
            if(num>0){
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param webTagTypeVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author shengQiang yu
     * @date 2019-6-7 20:44:17
     */
    public List<WebTagType> acceptList(WebTagTypeVo webTagTypeVo){
        Example example = new Example(WebTagType.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(webTagTypeVo.getRemoveTag())){
            criteria.andEqualTo("removeTag",webTagTypeVo.getRemoveTag());
        }
        if(StringUtil.isNotEmpty(webTagTypeVo.getName())){
            criteria.andLike("name","%"+webTagTypeVo.getName()+"%");
        }
        if(StringUtil.isNotEmpty(webTagTypeVo.getUserId())){
            criteria.andEqualTo("userId",webTagTypeVo.getUserId());
        }
        if(StringUtil.isNotEmpty(webTagTypeVo.getShowType())){
            criteria.andEqualTo("showType",webTagTypeVo.getShowType());
        }
        if(StringUtil.isNotEmpty(webTagTypeVo.getTagType())){
            criteria.andEqualTo("tagType",webTagTypeVo.getTagType());
        }
        example.setOrderByClause(" sort desc, update_time desc");
        return webTagTypeDao.selectByExample(example);
    }
}
